using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public enum DarkArenaStatus
{
    Idle,//Phase Host / Join 
    SelectClass,//Lancer jeu selection de perso
	SelectSpell, //selection spell
	Fight//On se met sur la gueule
}

public class DarkArena : MonoBehaviour {
	
	public GameManager gameManager;
	public GameRessource gameRessource;
	
	public TextAsset xmlPersonnage;
	public TextAsset xmlSpells;
	
	public Hud hud;
	public GameMenus gameMenus;
	public DisplayRound displayround;
	
	public DarkArenaStatus darkArenaStatus = DarkArenaStatus.Idle;
	
	private Personnage Perso1;
	private Personnage Perso2;
	
	//Les objets drag'ndrop
	//
	
	
	void Awake(){
		gameRessource = new GameRessource(xmlPersonnage,xmlSpells);
	}
	
	public void Clean(){
		Perso1=null;
		Perso2=null;
	}
	

	
	public void MajHUD(){
		displayround.ChiffreVieJ1.Text = Perso1.Pv.ToString();
		displayround.ChiffreRessourceJ1.Text = Perso1.NbRessource.ToString();
		
		displayround.ChiffreVieJ2.Text = Perso2.Pv.ToString();
		displayround.ChiffreRessourceJ2.Text = Perso2.NbRessource.ToString();
	}
	
	public Dictionary<int, Spells> CreateEffectSpell(Personnage Perso, Dictionary<int, int> ListSpellPerso){
		Dictionary<int, Spells> EffectSpell = new Dictionary<int, Spells>();
		int NBtour =-1;
		//Un round fait 10 tours
		//
		for(int i=0;i<10;i++){
			Debug.LogWarning("Tour "+i);
			if(ListSpellPerso.ContainsKey(i)){
				if(gameRessource.spellsDictionary[ListSpellPerso[i]].Instant==0){//Si instant
					EffectSpell.Add(i,gameRessource.spellsDictionary[ListSpellPerso[i]]);
					NBtour = NBtour + gameRessource.spellsDictionary[ListSpellPerso[i]].NbTour;
				}
				else{
					NBtour = NBtour + gameRessource.spellsDictionary[ListSpellPerso[i]].NbTour;
					EffectSpell.Add(NBtour,gameRessource.spellsDictionary[ListSpellPerso[i]]);
				}
			}
		}
		return EffectSpell;
	}
	
	public Dictionary<int, Spells> CreateListSpell(Personnage Perso, Dictionary<int, int> ListSpellPerso){
		Dictionary<int, Spells> ListEffectSpell = new Dictionary<int, Spells>();
		int NBtour =-1;
		//Un round fait 10 tours
		//
		for(int i=0;i<ListSpellPerso.Count;i++){
			if(ListSpellPerso.ContainsKey(i)){
				ListEffectSpell.Add(i,gameRessource.spellsDictionary[ListSpellPerso[i]]);
			}
		}
		return ListEffectSpell;
	}
	
	public IEnumerator Round(Dictionary<int, int> ListSpellPerso1,Dictionary<int, int> ListSpellPerso2,Personnage _Perso1,Personnage _Perso2){
		List<Personnage> ListPersoDead = new List<Personnage>();
		// si les perso n'existent pas
		if ( Perso1==null && Perso2==null)
		{
			Perso1 = CreatePersonnage(_Perso1.Id);
			Perso2 = CreatePersonnage(_Perso2.Id);
		}
		MajHUD();
		Dictionary<int, Spells> CreateEffectSpell1 = CreateEffectSpell(Perso1,ListSpellPerso1);
		Dictionary<int, Spells> CreateEffectSpell2 = CreateEffectSpell(Perso2,ListSpellPerso2);
		Dictionary<int, Spells> CreateListSpellJ1 = CreateListSpell(Perso1,ListSpellPerso1);
		Dictionary<int, Spells> CreateListSpellJ2 = CreateListSpell(Perso2,ListSpellPerso2);
		bool doActionJ1 = false;
		bool doActionJ2 = false;
		
		displayround.setDisplayRound(CreateListSpellJ1, CreateListSpellJ2, CreateEffectSpell1, CreateEffectSpell2, Perso1, Perso2);
		
		int[] DamageDotPerso1 = new int[10]{0,0,0,0,0,0,0,0,0,0};
		int[] DamageDotPerso2 = new int[10]{0,0,0,0,0,0,0,0,0,0};
		bool bAnnule = true;
		//Un round fait 10 tours
		//
		Debug.LogWarning("****************************DEBUT ROUND*********************************************");
		for(int i=0;i<10;i++){
			Debug.LogWarning("****************************DEBUT TOUR "+(i+1)+" *********************************************");
			//
			if(ListSpellPerso1.ContainsKey(i)){
				Perso1.DebutAction(Perso2, gameRessource.spellsDictionary[ListSpellPerso1[i]], ref DamageDotPerso1, i);
			}
			
			if(ListSpellPerso2.ContainsKey(i)){
				Perso2.DebutAction(Perso1, gameRessource.spellsDictionary[ListSpellPerso2[i]], ref DamageDotPerso2, i);
			}

			//On check si double interruption
			//
			if(CreateEffectSpell1.ContainsKey(i) && CreateEffectSpell2.ContainsKey(i)){
				bAnnule = AnnuleAction(Perso1,Perso2,CreateEffectSpell1.ContainsKey(i),CreateEffectSpell2.ContainsKey(i),CreateEffectSpell1[i],CreateEffectSpell2[i]);
			}
			
			if(bAnnule){//Si les 2 joueurs font une Interruption ou un contre alors on ne fait rien
				if(CreateEffectSpell1.ContainsKey(i)){
					if(Perso1.FinAction(Perso2)){
						gameMenus.InfoNeutralChat(Perso1.GetMessage);
						Perso2.subitAction(ref Perso1.currentSpell);
						gameMenus.InfoNeutralChat(Perso2.GetMessage);
						doActionJ1 = true;
					}
				}
				if(CreateEffectSpell2.ContainsKey(i)){
					if(Perso2.FinAction(Perso1)){
						gameMenus.InfoNeutralChat(Perso2.GetMessage);
						Perso1.subitAction(ref Perso2.currentSpell);
						gameMenus.InfoNeutralChat(Perso1.GetMessage);
						doActionJ2 = true;
					}
				}
			}
			Perso1.subitAction(Perso2.currentSpell, DamageDotPerso2[i]);
			Perso2.subitAction(Perso1.currentSpell, DamageDotPerso1[i]);
			Debug.LogWarning("PV "+Perso1.Nom+" / "+Perso1.Pv+" shield : "+Perso1.Shield+", Ressources: "+Perso1.NbRessource);
			Debug.LogWarning("PV "+Perso2.Nom+" / "+Perso2.Pv+" shield : "+Perso2.Shield+", Ressources: "+Perso2.NbRessource);
			Debug.LogWarning("****************************FIN TOUR "+(i+1)+" *********************************************");
			
			if(doActionJ1 && doActionJ2){
				yield return StartCoroutine(hud.PlayTour(i,1));
			}
			else{
				yield return StartCoroutine(hud.PlayTour(i,0));
			}
			yield return StartCoroutine(displayround.DamageDot(DamageDotPerso1[i],displayround.J2,displayround.GainPerteBarriereJ2 ,displayround.ChiffreBarriereJ2,displayround.PerteVieJ2, displayround.ChiffreVieJ2,displayround.FlashJ2));
			yield return StartCoroutine(displayround.DamageDot(DamageDotPerso2[i],displayround.J1,displayround.GainPerteBarriereJ1 ,displayround.ChiffreBarriereJ1,displayround.PerteVieJ1, displayround.ChiffreVieJ1,displayround.FlashJ1));
			doActionJ1 = false;
			doActionJ2 = false;
			MajHUD();
			
			ListPersoDead = new List<Personnage>(CheckDead());
			if(ListPersoDead.Count>0){
				i=20;
				foreach(Personnage p in ListPersoDead){
					Debug.LogError("Le "+p.Nom+" est mort.");
				}
				
			}
		}
		//Executer les fonction de fin de round de chaque classe, exemple recharge de 70 mana pour wizard
		//
		Perso1.EndRound();
		Perso2.EndRound();
		
		Debug.LogWarning("PV "+Perso1.Nom+" / "+Perso1.Pv+" shield : "+Perso1.Shield+", Ressources: "+Perso1.NbRessource);
		Debug.LogWarning("PV "+Perso2.Nom+" / "+Perso2.Pv+" shield : "+Perso2.Shield+", Ressources: "+Perso2.NbRessource);
		Debug.LogWarning("****************************FIN ROUND*********************************************");
		
		// on retourne sur la selection des sorts si pas debug
		if ( !gameManager.isDebugFightingScene)
		{
			Debug.LogWarning("go select spell");
			yield return new WaitForSeconds(1f);

			hud.QuitAllScenes();
			hud.InitializeSelectSpells();
		}
	}
	public Personnage[] CheckDead(){
		List<Personnage> ListPersoDead = new List<Personnage>();
		if(Perso1.Pv<=0){
			ListPersoDead.Add(Perso1);
		}
		if(Perso2.Pv<=0){
			ListPersoDead.Add(Perso2);
		}
		return ListPersoDead.ToArray();
	}
	
	public bool AnnuleAction(Personnage Perso1 ,Personnage Perso2 ,bool containskey1 ,bool containskey2 , Spells Spell1, Spells Spell2){
		if(Perso1.currentSpell!=null && Perso2.currentSpell != null){
			if(Perso1.currentSpell.Type == "Interrupt" && Perso2.currentSpell.Type == "Interrupt" && containskey1 && containskey2 && Spell1.Type == "Interrupt" && Spell2.Type == "Interrupt"){
				//Debug.LogWarning("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Double interruption &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");	
				return false;
			}else if(Perso1.currentSpell.Type == "Contre" && Perso2.currentSpell.Type == "Contre" && containskey1 && containskey2 && Spell1.Type == "Contre" && Spell2.Type == "Contre"){
				//Debug.LogWarning("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Double Contre &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");	
				return false;
			}else{
				return true;
			}
		}
		else{
			return true;	
		}
	}
	public Personnage CreatePersonnage(int idClass){
		if(idClass==1){
			Personnage Perso = new Warrior(idClass,"Warrior",200,0,0,null,gameRessource.TableauTextureClass[0]);
			return Perso;
		}
		if(idClass==2){
			Personnage Perso = new Wizard(idClass,"Wizard",175,0,100,null,gameRessource.TableauTextureClass[1]);
			return Perso;
		}
		if(idClass==3){
			Personnage Perso = new Thief(idClass,"Thief",185,0,100,null,gameRessource.TableauTextureClass[2]);
			return Perso;
		}
		return null;
	}	
}
